package com.design.document.service.impl;

import com.design.document.common.Const;
import com.design.document.common.ResponseService;
import com.design.document.entity.User;
import com.design.document.mapper.UserMapper;
import com.design.document.service.UserService;
import com.design.document.util.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service("userService")
public class UserServiceImpl implements UserService {


    private UserMapper userMapper;
    @Override
    public ResponseService<User> login(String username, String password) {

        int count = userMapper.checkUserName(username);

        if(count == 0){
            return ResponseService.createByErrorMessage("用户名不存在");
        }
        // md5 加密
        String MD5PW = MD5Util.MD5EncodeUtf8(password);

        User user = userMapper.selectLogin(username,MD5PW);
        if(user == null){
            return ResponseService.createByErrorMessage("密码错误");
        }
        //密码质变为空为了安全
        user.setPassword(StringUtils.EMPTY);
        return ResponseService.createBySuccess("成功登录",user);
    }

    @Override
    public ResponseService<String> regist(User user) {

        //到这的话就是可以注册的,把密码用Md5加密存入数据库中
        String md5password = MD5Util.MD5EncodeUtf8(user.getPassword());

        user.setPassword(md5password);
        //设置role 普通用户
        user.setRole(Const.Role.ROLE_STUDENT);

        int resultCount = userMapper.insert(user);
        if(resultCount == 0){
            return ResponseService.createByErrorMessage("注册失败");
        }
        return ResponseService.createBySuccessMessage("注册成功!");
    }

    @Override
    public ResponseService<User> findByUserName(String username) {
        return null;
    }
}
